<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="missionStatus">
        <el-select v-model="queryParams.missionStatus" placeholder="审核状态" clearable>
          <el-option
            v-for="dict in dict.type.mission_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入任务标题"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['examine:task:add']"
        >发布任务
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commonList">
      <el-table-column label="任务标题" align="center" prop="title"/>
      <el-table-column label="截止时间" align="center" prop="endTime"  width="168px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="examineUserName"  width="100px"/>
      <el-table-column label="审核人" align="center" prop="apprUserName"  width="100px"/>
      <el-table-column label="任务状态" align="center" prop="missionStatus" width="80px">
        <template slot-scope="scope">
          {{ getDictValue(scope.row.missionStatus) }}
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="apprTime"  width="168px"/>
      <el-table-column label="发布时间" align="center" prop="pubTime"  width="168px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="120px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['examine:task:edit']"
            v-if="scope.row.missionStatus === -1"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['examine:task:query']"
            v-if="scope.row.missionStatus !== -1"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listTask, delTask} from "@/api/examine/task/api";
import quarter from "@/views/tool/quarter";
import halfyear from "@/views/tool/halfyear";

export default {
  name: "Index",
  dicts: ['examine_cycle', 'examine_appr_state', 'mission_status'],
  components: {
    quarter,
    halfyear,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通用考核表格数据
      commonList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examineUserName: undefined,
        examineCycle: undefined,
        pickCycle: undefined,
        approveStatus: undefined,
        title: undefined
      },
      examineYearOptions: {
        disabledDate: (time) => {
          const date = new Date()
          const year = date.getFullYear()
          const timeYear = time.getFullYear()
          return year > timeYear + 3
        }
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询通用考核列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.commonList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDictValue(data) {
      const obj = this.dict.type.mission_status.find(d => Number(d.value) === data)
      if (obj) {
        return obj.label
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: "/task/add"});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({path: "/task/add", query: {id: row.id}});
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.$router.push({path: "/task/view", query: {id: row.id}});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除通用考核周期为"' + row.pickCycleText + '"的数据？删除后无法恢复').then(function () {
        return delTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/common/export', {
        ...this.queryParams
      }, `common_${new Date().getTime()}.xlsx`)
    },
    // 季度选择回调
    getQuarter(val) {
      this.queryParams.pickCycle = val
    },
    // 半年度选择回调
    getHalfyear(val) {
      this.queryParams.pickCycle = val
    }
  }
};
</script>
